<%
local api = require "luci.model.cbi.passwall.api.api"
local dsp = require "luci.dispatcher"

function get_luci_version()
	local ipkg  = require "luci.model.ipkg"
	local package_name = "luci-app-passwall"
	local package_info = ipkg.info(package_name) or {}
	if next(package_info) ~= nil then
		return package_info[package_name]["Version"]
	end
	return ""
end

local passwall_version = get_luci_version()
-%>

<script type="text/javascript">
	//<![CDATA[
	var passwallInfo;
	var tokenStr = '<%=token%>';
	var noUpdateText = '<%:It is the latest version%>';
	var updateSuccessText = '<%:Update successful%>';
	var clickToUpdateText = '<%:Click to update%>';
	var inProgressText = '<%:Updating...%>';
	var unexpectedErrorText = '<%:Unexpected error%>';
	var updateInProgressNotice = '<%:Updating, are you sure to close?%>';
	var downloadingText = '<%:Downloading...%>';
	var decompressioningText = '<%:Unpacking...%>';
	var movingText = '<%:Moving...%>';

	window.onload = function() {
		var passwallCheckBtn = document.getElementById('_passwall-check_btn');
		var passwallDetailElm = document.getElementById('_passwall-check_btn-detail');
	};

	function addPageNotice_luci() {
		window.onbeforeunload = function(e) {
			e.returnValue = updateInProgressNotice;
			return updateInProgressNotice;
		};
	}

	function removePageNotice_luci() {
		window.onbeforeunload = undefined;
	}

	function onUpdateSuccess_luci(btn) {
		alert(updateSuccessText);

		if(btn) {
			btn.value = updateSuccessText;
			btn.placeholder = updateSuccessText;
			btn.disabled = true;
		}

		window.setTimeout(function() {
			window.location.reload();
		}, 1000);
	}

	function onRequestError_luci(btn, errorMessage) {
		btn.disabled = false;
		btn.value = btn.placeholder;

		if(errorMessage) {
			alert(errorMessage);
		}
	}

	function onBtnClick_luci(btn) {
		if(passwallInfo === undefined) {
			checkUpdate_luci(btn);
		} else {
			doUpdate_luci(btn);
		}
	}

	function checkUpdate_luci(btn) {
		btn.disabled = true;
		btn.value = inProgressText;

		addPageNotice_luci();

		var ckeckDetailElm = document.getElementById(btn.id + '-detail');

		XHR.get('<%=dsp.build_url("admin/vpn/passwall/luci_check")%>', {
			token: tokenStr,
			arch: ''
		}, function(x,json) {
			removePageNotice_luci();

			if(json.code) {
				passwallInfo = undefined;
				onRequestError_luci(btn, json.error);
			} else {
				if(json.update) {
					passwallInfo = json;
					btn.disabled = false;
					btn.value = clickToUpdateText;
					btn.placeholder = clickToUpdateText;
					
					if(ckeckDetailElm) {
						var urlNode = '';
						if(json.version) {
							urlNode = '<em style="color:red;">最新版本号：' + json.version + '</em>';
							if(json.url && json.url.html) {
								urlNode = '<a href="' + json.url.html + '" target="_blank">' + urlNode + '</a>';
							}
						}
						ckeckDetailElm.innerHTML = urlNode;
					}
				} else {
					btn.disabled = true;
					btn.value = noUpdateText;
				}
			}
		},300);
	}

	function doUpdate_luci(btn) {
		btn.disabled = true;
		btn.value = downloadingText;

		addPageNotice_luci();

		var updateUrl = '<%=dsp.build_url("admin/vpn/passwall/luci_update")%>';
		// Download file
		XHR.get(updateUrl, {
			token: tokenStr,
			url: passwallInfo ? passwallInfo.url.download : '',
			save: true
		}, function(x,json) {
			removePageNotice_luci();
			if(json.code) {
				onRequestError_luci(btn, json.error);
			} else {
				onUpdateSuccess_luci(btn);
			}
		},300)
	}
	//]]>
</script>

<div class="cbi-value">
	<label class="cbi-value-title">PassWall
		<%:Version%>
	</label>
	<div class="cbi-value-field">
		<div class="cbi-value-description">
			<span>【 <%=passwall_version%>】</span>
			<input class="cbi-button cbi-input-apply" type="button" id="_passwall-check_btn" onclick="onBtnClick_luci(this);" value="<%:Manually update%>" />
			<span id="_passwall-check_btn-detail"></span>
		</div>
	</div>
</div>